import 'package:environmental_les_app/common/style/colours.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'package:flutter_clx_base/utils/string_util.dart';

class CLXSearchBar extends StatefulWidget implements PreferredSizeWidget {
  final Function? onCallback;
  final String? hintText;
  final Widget? leading;
  final Color? backgroundColor;
  final Color? foregroundColor;
  final bool primary;
  final Color? searchBackgroundColor;
  final Color? inputTextColor;
  final Color? hintTextColor;
  final Color? rightTextColor;
  final Color? searchIconColor;
  final double? searchIconSize;
  final Function? onFocusListener;
  final String? rightText;
  final Function? onRightBtnCallback;
  final bool? enabled;
  final GestureTapCallback? onTap;
  final EdgeInsetsGeometry? margin;
  final bool automaticallyImplyLeading;
  final TextEditingController? textEditingController;
  final double? leadingWidth;

  const CLXSearchBar({
    super.key,
    this.onCallback,
    this.hintText = "请输入搜索内容",
    this.leading,
    this.backgroundColor,
    this.foregroundColor,
    this.primary = true,
    this.searchBackgroundColor = Colours.bgF2F3F5,
    this.inputTextColor = Colours.text969,
    this.hintTextColor = Colours.textC9CDD4,
    this.rightTextColor,
    this.onFocusListener,
    this.rightText,
    this.onRightBtnCallback,
    this.searchIconColor,
    this.searchIconSize,
    this.margin,
    this.automaticallyImplyLeading = true,
    this.textEditingController,
    this.leadingWidth,
    this.enabled,
    this.onTap,
  });

  @override
  State<CLXSearchBar> createState() => _SearchBarState();

  @override
  Size get preferredSize => const Size(double.infinity, 55.0);
}

class _SearchBarState extends State<CLXSearchBar> {
  late TextEditingController _searchInfoController;
  final _searchFocusNode = FocusNode();

  @override
  void initState() {
    _searchFocusNode.addListener(() {
      if (widget.onFocusListener != null) {
        widget.onFocusListener!(_searchFocusNode.hasFocus);
      }
    });
    _searchInfoController =
        widget.textEditingController ?? TextEditingController();

    _searchInfoController.addListener(() {
      setState(() {});
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: widget.backgroundColor,
      foregroundColor: widget.foregroundColor,
      elevation: 0.0,
      titleSpacing: 0.0,
      primary: widget.primary,
      leading: widget.leading,
      leadingWidth: widget.leadingWidth,
      automaticallyImplyLeading: widget.automaticallyImplyLeading,
      title: InkWell(
        onTap: widget.onTap,
        child: Row(
          children: <Widget>[
            Expanded(
              child: Container(
                margin: widget.margin,
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(6.0)),
                  color: widget.searchBackgroundColor,
                ),
                child: Row(
                  children: <Widget>[
                    hGap5,
                    Expanded(
                      child: TextField(
                        maxLines: 1,
                        enabled: widget.enabled,
                        focusNode: _searchFocusNode,
                        textAlignVertical: TextAlignVertical.center,
                        style: TextStyle(
                            textBaseline: TextBaseline.alphabetic,
                            fontSize: 15,
                            color: widget.inputTextColor),
                        textInputAction: TextInputAction.search,
                        controller: _searchInfoController,
                        onSubmitted: (value) => _search(),
                        decoration: InputDecoration(
                          suffixIcon: (_searchInfoController.text.isNotEmpty &&
                                  widget.enabled == true)
                              ? InkWell(
                                  onTap: () {
                                    _searchInfoController.clear();
                                  },
                                  child: Container(
                                    width: 15.0,
                                    height: 15.0,
                                    decoration: const BoxDecoration(
                                      shape: BoxShape.circle,
                                      color: Colours.textC9CDD4,
                                    ),
                                    child: Icon(
                                      Icons.clear,
                                      color: widget.searchIconColor ??
                                          Colors.grey.shade300,
                                      size: 10.0,
                                    ),
                                  ),
                                )
                              : null,
                          prefixIconConstraints: const BoxConstraints(
                              minWidth: 40.0, minHeight: 20.0),
                          prefixIcon: Icon(
                            Icons.search_rounded,
                            size: widget.searchIconSize ?? 20.0,
                            color: widget.searchIconColor ?? Colours.textC5C7D3,
                          ),
                          suffixIconConstraints: const BoxConstraints(
                              minWidth: 40.0, minHeight: 15.0),
                          contentPadding: const EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 8.0),
                          isDense: true,
                          counterText: "",
                          hintText: widget.hintText,
                          hintStyle: TextStyle(
                              fontSize: 15.0,
                              color: widget.hintTextColor ??
                                  const Color(0xFF999999)),
                          border: InputBorder.none, //去掉下划线
                          //hintStyle: TextStyles.textGrayC14
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            widget.rightText.isNullOrEmpty()
                ? const SizedBox(width: 15.0)
                : InkWell(
                    onTap: () {
                      widget.onRightBtnCallback != null
                          ? widget.onRightBtnCallback?.call()
                          : _search();
                    },
                    child: Container(
                      width: 60.0,
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                        widget.rightText ?? "搜索",
                        style: TextStyle(
                            color: widget.rightTextColor ??
                                const Color(0xFFFFFFFF),
                            fontSize: 14.0),
                      ),
                    ),
                  )
          ],
        ),
      ),
    );
  }

  /// 搜索
  void _search() {
    KeyBoardUtils.hideKeyboard();
    if (widget.onCallback != null) {
      widget.onCallback!(getControllerString(_searchInfoController));
    }
  }
}
